package Simple2;

import java.util.ArrayList;
import java.util.Arrays;


public class GoogleInterview_CheckDuplication_13254ChangeSequence {
	
	/**
	 * Change sequence to 13254.....1<3>2<5>4
	 * @param input		an unsorted array
	 * @return			a sequenced array
	 */
	public static int[] ChangeSequence(int[] input){
		
		int flag=0;
		if(input.length<2) return input;
		for(int i=1;i<input.length;i++){
			if(flag==0){
				if(input[i]<input[i-1]){
					int temp=input[i];
					input[i]=input[i-1];
					input[i-1]=temp;
				}
				flag=1;
			}
			else {
				if(input[i]>input[i-1]){
					int temp=input[i];
					input[i]=input[i-1];
					input[i-1]=temp;
				}			
				flag=0;
			}
		}
		return input;
	}
	
	
	public static boolean isDuplicate(String input){
		
		char[] ch=input.toCharArray();
		boolean to_return = false;
		ArrayList<Character> cur = new ArrayList<>();
		if(ch.length<2) return to_return;
		
		for(int i=0;i<ch.length;++i){
			cur.add(ch[i]);
			if((ch.length % cur.size())!=0 || ch.length==cur.size())
				continue;
			else{
				int times = ch.length/cur.size();
				int len = i+1;
				char[] to_check = null;
				for(int j=1;j<times;j++){
					to_check = Arrays.copyOfRange(ch, len*j, len*j+len);
					if(isSame(cur, to_check)) to_return = true;
					else{
						to_return=false;
						break;
					}
				}
				if(to_return==true) break;
			}
		}
		return to_return;
	}
	
	
	public static boolean isSame(ArrayList<Character> a, char[] b){
		if(a.size()!=b.length) return false;
		for(int i=0;i<a.size();i++){
			if(a.get(i)!=b[i])
				return false;
		}
		return true;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] test = {1,2,3,4,6};
		test = ChangeSequence(test);
		for(int i=0;i<test.length;i++){
			System.out.print(test[i]+" ");
		}
		
		String test2 = "abcabcabc";
		boolean a =isDuplicate(test2);
		System.out.println("\n"+a);
		
		
	}

}
